﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
namespace EpicBall.ScreenSystem
{
    public enum ScreenState
    {
        TransitionOn,
        Active,
        TransitionOff,
        Hidden,
    }
    public abstract class GameScreen
    {
        protected int _screenWidth, _screenHeight;
        protected double screenElapsedTime = 0;
        protected bool isExiting = false;
        public int Width { get { return _screenWidth; } }
        public int Height { get { return _screenHeight; } }

        protected Song screenSong = null;
        protected bool songPlaying = false;

        protected Texture2D backgroundTexture;

        public GameScreen() : this(0, 0) { }
        public GameScreen(int screenWidth, int screenHeight)
        {
            _screenWidth = screenWidth;
            _screenHeight = screenHeight;
            ScreenState = ScreenState.TransitionOn;
            TransitionOffTime = TimeSpan.Zero;
            TransitionOnTime = TimeSpan.Zero;
            TransitionAlpha = 0f;
        }

        public GameStack GameStack { get; set; }
        /// <summary>
        /// The current state of the screen
        /// </summary>
        public ScreenState ScreenState { get; protected set; }
        
        /// <summary>
        /// How much time to fade (transition) in
        /// </summary>
        public TimeSpan TransitionOnTime { get; protected set; }

        /// <summary>
        /// How much time to fade (transition) out
        /// </summary>
        public TimeSpan TransitionOffTime { get; protected set; }

        /// <summary>
        /// How far along the transition: used to update screen alpha
        /// </summary>
        public float TransitionAlpha { get; protected set; }

        public virtual void Update(GameTime gameTime) {
            screenElapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (ScreenState == ScreenState.TransitionOn)
            {
                UpdateTransition(gameTime);
                if(TransitionAlpha == 1) 
                {
                    ScreenState = ScreenState.Active;
                }
            }
            if (ScreenState == ScreenState.TransitionOff)
            {
                UpdateTransition(gameTime);
                if (TransitionAlpha == 0)
                {
                    ScreenState = ScreenState.Hidden;
                }
            }

            if (screenSong != null && songPlaying == false)
            {
                MediaPlayer.Play(screenSong);
                songPlaying = true;
                MediaPlayer.IsRepeating = true;
            }
        }

        public virtual void LoadContent() 
        {

            if(_screenWidth == 0) 
            {
                _screenWidth = (int)GameStack.Dimensions.X;
                _screenHeight = (int)GameStack.Dimensions.Y;
            }

        }
        public virtual void UnloadContent() { }

        public virtual void UpdateTransition(GameTime gameTime)
        {
            //Fade in
            if (ScreenState == ScreenState.TransitionOn)
            {
                //Instantly set alpha to 100% if time required for transition = 0
                if (TransitionOnTime == TimeSpan.Zero)
                {
                    TransitionAlpha = 1f;
                }
                else
                {
                    TransitionAlpha = (float)(screenElapsedTime / TransitionOnTime.TotalMilliseconds);
                    TransitionAlpha = MathHelper.Clamp(TransitionAlpha, 0f, 1f);
                }
            }
            else
            {
                //Instantly set alpha to 0% if time required for transition = 0
                if (TransitionOnTime == TimeSpan.Zero)
                {
                    TransitionAlpha = 0f;
                }
                else
                {
                    TransitionAlpha = 1f - (float)(gameTime.ElapsedGameTime.TotalMilliseconds / TransitionOnTime.TotalMilliseconds);
                    TransitionAlpha = MathHelper.Clamp(TransitionAlpha, 0f, 1f);
                }
            }
        }
        public virtual void Draw(GameTime gameTime) { }
    }
}
